package com.zhiyou.app.common.vo.resp;

import lombok.Getter;

import java.io.Serializable;

/**
 * 相应返回
 *
 * @param <T>
 * @author jhb
 */
@Getter
public class Response<T> implements Serializable {


    /**
     * 响应码,常见返回码：000-操作成功  001-操作失败
     */
    private final String code;
    /**
     * 响应消息
     */
    private final String msg;
    /**
     * 响应数据
     */
    private T data;

    /**
     * 默认成功，无数据
     */
    public Response() {
        this.code = ResponseCodeDefault.SUCCESS.getCode();
        this.msg = ResponseCodeDefault.SUCCESS.getMsg();
    }

    /**
     * 预定义code及msg，无数据
     *
     * @param responseCode
     */
    public Response(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();

    }

    /**
     * 自定义code及msg，无数据
     *
     * @param code
     * @param msg
     */
    public Response(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 默认成功，有数据
     *
     * @param data
     */
    public Response(T data) {
        this(ResponseCodeDefault.SUCCESS, data);
    }

    /**
     * 预定义code及msg，有数据
     *
     * @param responseCode
     * @param data
     */
    public Response(ResponseCode responseCode, T data) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();
        this.data = data;
    }

    /**
     * 自定义code及msg，有数据
     *
     * @param code
     * @param msg
     * @param data
     */
    public Response(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    /**
     * 成功：默认code及msg，无data
     *
     * @return
     */
    public static <T> Response<T> success() {
        return new Response();
    }

    /**
     * 成功：默认code，自定义msg，无data
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> success(String msg) {
        return new Response<>(ResponseCodeDefault.SUCCESS.getCode(), msg);
    }

    /**
     * 失败，默认code及msg，无data
     *
     * @return
     */
    public static <T> Response<T> failed() {
        return new Response<>(ResponseCodeDefault.FAILED);
    }

    /**
     * 失败，默认code，自定义msg，无data
     *
     * @param msg
     * @return
     */
    public static <T> Response<T> failed(String msg) {
        return new Response<>(ResponseCodeDefault.FAILED.getCode(), msg);
    }


    /**
     * 判断是否成功
     *
     * @return
     */
    public boolean isSuccess() {
        return ResponseCodeDefault.SUCCESS.getCode().equals(code);
    }

    /**
     * 添加数据
     *
     * @param t   数据
     * @param <T>
     * @return
     */
    public static <T> Response<T> successWithData(T t) {
        return new Response<>(t);
    }

}

